package traveldream.ejb.sessionBean;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import traveldream.ejb.DTO.HotelDTO;
import traveldream.ejb.DTO.PackageDTO;
import traveldream.ejb.entities.CustomizedPackage;
import traveldream.ejb.entities.DefaultPackage;
import traveldream.ejb.entities.Hotel;
import traveldream.ejb.interfaces.NotFindException;
import traveldream.ejb.interfaces.PackageBeanLocal;

/**
 * Session Bean implementation class PackageBean
 */
@Stateless
public class PackageBean implements PackageBeanLocal {

	@PersistenceContext
	private EntityManager em;
    /**
     * Default constructor. 
     */
    public PackageBean() {
        // TODO Auto-generated constructor stub
    }
    
    public List<PackageDTO> allPackage(){
    	List<DefaultPackage> dP = em.createNamedQuery(DefaultPackage.FIND_ALL).getResultList();
    	List<PackageDTO> pDTO = new ArrayList<PackageDTO>();
    	
    	for(DefaultPackage d : dP){
    		pDTO.add(d.convertToDTO());
    	}
    	return pDTO;
    }
    
    public PackageDTO find(int id) throws NotFindException{
    	DefaultPackage dP = em.find(DefaultPackage.class, id);
    	try{
    		return dP.convertToDTO();
    	}
    	catch(NullPointerException e){
    		throw new NotFindException();
    	}
    }

    public PackageDTO findCustom(int id){
    	CustomizedPackage cP = em.find(CustomizedPackage.class, id);
    	if(cP!=null){
    		System.out.println("entrato");
    		return cP.convertToDTO();
    		
    	}
    	return null;
    }
    
	@Override
	public void delete(int id) throws NotFindException{
		// TODO Auto-generated method stub
	try{
		DefaultPackage dP = em.find(DefaultPackage.class, id);
			dP.setInvalid(true);
	}
	catch(NullPointerException e){
		throw new NotFindException();
	}
	}

}
